package com.happyscan138.makefun.Fragment;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.happyscan138.makefun.BaseAdapter.persional_VIDEO;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.VideoDetail;
import com.happyscan138.makefun.adapter.Persional_VideoAdapter;
import com.happyscan138.makefun.base.LoadListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static android.content.Context.MODE_PRIVATE;
import static com.happyscan138.makefun.Urls.Urls.base;
import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/1/1.
 */

public class MydiscussVideo extends Fragment implements LoadListView.IloadListener {
    private List<persional_VIDEO> ListData;
    private LoadListView persional_video;
    private SharedPreferences preferences;
    private String access_key;
    private Intent intent;
    private  persional_VIDEO data;
    private int  COM_ID_INDEX ;
    private ArrayList<Integer> ID;
    private Handler handler;
    private Persional_VideoAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.persional_video,null);
        client=new AsyncHttpClient();
        params=new RequestParams();
        preferences = getActivity().getSharedPreferences("UserInfo", MODE_PRIVATE);
        access_key=preferences.getString("UserId","");
        initView(view);
        getAdapterView(view);
        persional_video.setInterface(this);
        return view;
    }
    public void initView(View view){
        persional_video=(LoadListView)view.findViewById(R.id.persional_video);
    }
    public View getAdapterView(View view) {
        log.e("discusscideo","discusscideo");
        ListData = new ArrayList<>();
        ID=new ArrayList<Integer>();
        params.put("access_key",access_key);
        params.put("page_count","10");
        params.put("com_id_index","0");
        client.post(Urls.myDiscussSaobo,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                String jsonStr=response.toString();
                log.e("discusscideojsonStr",jsonStr);
                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject objectData=jsonArray.getJSONObject(i);
                        log.e("objectData",objectData.toString());
                        COM_ID_INDEX = objectData.getInt("com_id");
                        ID.add(COM_ID_INDEX);
                        data=new persional_VIDEO();
                        log.e("icondata",data.toString());
                        data.head= base+objectData.getString("img_path");
                        log.e("icon",data.head.toString());
                        data.time=objectData.getString("com_date");

                        data.comment=objectData.getString("comment");
                        data.nicheng=objectData.getString("nick_name");
                        data.resource= base+objectData.getString("resource");
                        data.content=objectData.getString("content");
                        data.problem_id=objectData.getString("problem_id");
                        ListData.add(data);

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                adapter=new Persional_VideoAdapter(getActivity(), ListData);
                persional_video.setAdapter(adapter);
                persional_video.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        intent = new Intent();
                        data = ListData.get(position);
                        intent.putExtra("problem_id", data.problem_id);
                        log.e("problem_id",data.problem_id);
                        intent.setClass(getActivity(), VideoDetail.class);
                        startActivity(intent);

                    }
                });

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                log.e("responseString",responseString);
            }
        });

        return view;
    }

    @Override
    public void onLoad() {
        handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                params.put("access_key", access_key);
                params.put("page_count", "10");
                params.put("com_id_index", ID.get(ID.size() - 1));
                RequestHandle handle = client.post(Urls.MyCollection, params, new JsonHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String jsonStr = response.toString();
                        log.e("jsonStr", jsonStr);

                        try {
                            JSONObject jsonObject = new JSONObject(jsonStr);
                            JSONArray jsonArray = jsonObject.getJSONArray("data");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject objectData = jsonArray.getJSONObject(i);
                                log.e("objectData", objectData.toString());
                                data = new persional_VIDEO();
                                data.head = base + objectData.getString("img_path");
                                log.e("icon", data.head.toString());
                                data.time = objectData.getString("com_date");
                                data.comment = objectData.getString("comment");
                                data.name = objectData.getString("nick_name");
                                data.resource = base + objectData.getString("resource");
                                data.content = objectData.getString("content");
                                data.problem_id=objectData.getString("problem_id");
                                ListData.add(data);

                            }
                            log.e("listData", ListData.toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        adapter.notifyDataSetChanged();
                    }
   //SDSDSDS
                    @Override
                    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                        super.onFailure(statusCode, headers, responseString, throwable);
                        log.e("responseString", responseString);
                    }
                });
                persional_video.loadComplete();
            }
        },2000);
    }

}
